package com.web.user.member.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.base.kongjian.service.FooterPrintService;
import com.base.user.entity.Member;
import com.base.user.service.MemberService;
import com.mysteel.core.SpringContext;
import com.web.user.member.utils.CookieUtils;

import cn.mysteel.util.ObjectUtils;

public class SeeFilter implements Filter
{

	@Override
	public void init(FilterConfig filterConfig) throws ServletException
	{
		
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException
	{
		MemberService memberService = (MemberService) SpringContext.getBean("memberService");
		
		HttpServletRequest req= (HttpServletRequest) request;   
		HttpServletResponse resp= (HttpServletResponse) response;  
		
		Cookie[] ck = req.getCookies ();
		int flag = 0;
		if(ObjectUtils.notNull (ck))
		{
			for(Cookie cookie:ck)
			{
				if(cookie.getName ().equals ("member_id"))
				{
					flag = 1;
				}
			}
		}
		if(flag == 0)
		{
			resp.sendRedirect ("/login");
			
			return ;
		}
		String urlPath = req.getServletPath ();
		String[] str = urlPath.split ("/");
		if((ObjectUtils.notNull (str))&&str.length==3)
		{
			long id = Long.parseLong (str[str.length-1]);
			long memberId = Long.parseLong (CookieUtils.getCookieValue ((HttpServletRequest)request ,"member_id"));
			if(id==memberId)
			{
				request.setAttribute ("memberId" ,id);
				request.setAttribute ("type" ,1); //�����Լ��ռ�
				resp.sendRedirect ("/u/"+id);
				
				return;
			}else
			{
				Member m = memberService.getMemberById (id);
				if(ObjectUtils.notNull (m))
				{
					request.setAttribute ("memberId" ,id);
					request.setAttribute ("type" ,2); //������ʱ��˿ռ�
					boolean b = visit(id ,memberId ,(HttpServletRequest)request);
					if(b == true)
					{
						request.getRequestDispatcher("/kj/see/kongjian.html?memberId="+str[str.length-1]).forward(request, response);
					}else
					{
						//request.getRequestDispatcher("/kj/see/kongjian.html?memberId="+str[str.length-1]).forward(request, response);
					}
					
					return;
				}
			}
		}
		
		req.getRequestDispatcher("/basic/404.htm").forward(request, response);
			
		 return ;
	}

	@Override
	public void destroy()
	{
		
		
	}
	
	public boolean visit(long memberId,long visitor_MId,HttpServletRequest request)
	{
		FooterPrintService fPrintService = (FooterPrintService) SpringContext.getBean("footerPrintService");
		
		boolean b = fPrintService.canVisitor (memberId ,visitor_MId);
		
		if(b == true)
		{
			fPrintService.visit (memberId ,visitor_MId);
		}
		
		return b;
	}
	
}
